package com.produce.system.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.produce.common.persistence.Page;
import com.produce.common.util.ValidateUtils;
import com.produce.system.constans.SysUserConstants;
import com.produce.system.entity.SysUser;
import com.produce.system.service.SysUserService;
/**
 * 用户管理
 * @author WenPeng
 */
@Controller
@RequestMapping(value = "sysuser")
public class SysUserController {
	private Logger logger = Logger.getLogger(SysUserController.class);
	@Resource
	private SysUserService sysUserService;
	/**
	 * 初始化用户页面
	 */
	@RequestMapping(value = "init")
	public ModelAndView initUser(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) {
		logger.info("初始化用户页面");
		return new ModelAndView(SysUserConstants.INITUSER,modelMap);
	}
	/**
	 * 用户出列表信息
	 */
	@RequestMapping(value = "list")
	public ModelAndView listUser(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap,SysUser sysUser,Page<SysUser> page){
		logger.info("出用户列表");
		request.setAttribute("page", sysUserService.findByPage(sysUser, page));
		return new ModelAndView(SysUserConstants.USERLIST,modelMap);
	}
	/**
	 * 用户的新增和修改页面
	 */
	@RequestMapping(value = "user")
	public ModelAndView user(String userid ,HttpServletRequest request, ModelMap modelMap) {
		//查到user对象 到页面
		if(ValidateUtils.isNotEmpty(userid)){
			logger.info("进  用户  修改页面"+userid);
			SysUser user = sysUserService.getById(userid);
			request.setAttribute("user", user);
		}
		//userid 为空  进新增页面
		return new ModelAndView(SysUserConstants.USER,modelMap );
	}
	/**
	 * 新增用户 或 修改用户
	 */
	@RequestMapping(value = "save")
	@ResponseBody
	public String adduser(SysUser user,HttpServletRequest request ,ModelMap modelMap){
		JSONObject json = new JSONObject();
		try {
//			修改
			if(ValidateUtils.isNotEmpty(user.getUserid())){
				logger.info("修改用户开始"+user.getUserid());
				sysUserService.update(user);
			}
			//新增
			else{
				logger.info("新增用户开始");
				sysUserService.add(user);
			}
			json.put("result", "save_success");
		} catch (Exception e) {
			json.put("result", "save_fail");
		}
		return json.toString();
	}
	/**
	 * 删除用户操作
	 */
	@RequestMapping(value = "delete" )
	@ResponseBody
	public String delete(String userid){
		logger.info("删除用户开始");
		JSONObject json = new JSONObject();
		try {
			logger.info("userid----------"+userid);
			sysUserService.delete(userid);
			json.put("result", "delete_success");
		} catch (Exception e) {
			json.put("result", "delete_fail");
		}
		return json.toString();
	}
}
